//
//  LYVisitorView.swift
//  DSDweibo
//
//  Created by Mac on 15/9/8.
//  Copyright © 2015年 FSK. All rights reserved.
//

import UIKit
// Swift中如何定义协议: 必须遵守NSObjectProtocol
protocol visitorViewDelegate: NSObjectProtocol{

    func loginBtnWillClick()
    func regisiterBtnWillClick()

}

class LYVisitorView: UIView {
    
    
    // 定义一个代理属性
    // 代理属性需要weak修饰,防止循环引用
    weak var delegate:visitorViewDelegate?
    
    /// 方法确定当前是否是主页, 和主页显示内容
    func setupVisitorInfo(isHome:Bool, imageName: String, messageLabel: String){
    
        iconView.hidden = !isHome
        homeView.image = UIImage(named: imageName)
        titleLabel.text = messageLabel
        
    
        if isHome{
            
            // 创建动画
            let animal = CABasicAnimation(keyPath: "transform.rotation")
            
            animal.duration = 20
            animal.repeatCount = MAXFLOAT
            animal.toValue = 2 * M_PI
            
            // 这个属性默认为YES, 表示只要动画执行完毕就移除
            animal.removedOnCompletion = false
        
            iconView.layer.addAnimation(animal, forKey: nil)
        }
        
        
    }
    
    func loginBtnClick()
    {
      print(__FUNCTION__)
        
      delegate?.loginBtnWillClick()
    
    }
    
    func regisiterBtnClick()
    {
        print(__FUNCTION__)
        
        delegate?.regisiterBtnWillClick()
        
    }
    
    
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        /// 添加子控件
        addSubview(iconView)
        addSubview(maskBGView)
        addSubview(homeView)
        addSubview(titleLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        /// 布局子控件
        // 设置背景
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        maskBGView.xmg_Fill(self)
        
        // 设置小房子
        homeView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        
        // 设置文字
        titleLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
        
        let widthCons = NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 244)
        
        // 将属性添加到titleLabel上
        addConstraint(widthCons)
        
        
        // 设置按钮
        registerBtn.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: titleLabel, size: CGSize(width: 100, height: 30), offset: CGPoint(x: 0, y: 20))
        loginBtn.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: titleLabel, size: CGSize(width: 100, height: 30), offset: CGPoint(x: 0, y: 20))
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// 转盘
    private lazy var iconView: UIImageView = {
    
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return iv
    }()
    
    /// 图标
    private lazy var homeView: UIImageView = {
    
        let hv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return hv
    }()
    
    /// 文本
    private lazy  var titleLabel: UILabel = {
    
         let label = UILabel()
         label.numberOfLines = 0
         label.text = "今天天气不错,适合粗去旅游大反馈你看发放金额飞额"
         label.textColor = UIColor.darkGrayColor()
        return label
    }()
    
    
    /// 登录按钮
    private lazy var loginBtn: UIButton = {
    
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    /// 注册按钮
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "regisiterBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        }()

    private lazy var maskBGView: UIImageView = {
    
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return iv

    }()
    
    
}
